package com.lzs.bootsafety.common.oss;

import com.lzs.bootsafety.business.oss.entity.SysFile;
import com.lzs.bootsafety.business.oss.service.SysConfigService;
import com.lzs.bootsafety.business.oss.service.SysFileService;
import com.lzs.bootsafety.business.sys.entity.SysUser;
import com.lzs.bootsafety.common.oss.Exception.OssException;
import com.lzs.bootsafety.common.util.CheckIsNull;
import com.lzs.bootsafety.common.util.SpringContextAware;
import com.lzs.bootsafety.common.util.enums.SysConfig;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.Map;
/**
 * @author  zhanshi Liu
 * @date    2020/04/19
 * @website www.jhmsona.cn
 */
public class BaseFileUpload {
    private final Logger log = LoggerFactory.getLogger(getClass());
    FileApi getFileApi(String storageType){
        SysConfigService sysConfigService = SpringContextAware.getBean(SysConfigService.class);
        Map<String ,Object> sysConfigs = sysConfigService.getConfigs();
        if (StringUtils.isEmpty(storageType)){
            storageType=(String) sysConfigs.get(SysConfig.STOTAGE_TYPE.getKey());
        }
        FileApi fileApi=null;
        if (sysConfigs==null || StringUtils.isEmpty(storageType)){
            throw new OssException("当前未配置云存储信息");
        }
        switch (storageType){
            case "qiniu":
                String accessKey=String.valueOf(sysConfigs.get(SysConfig.QINIU_ACCESS_KEY.getKey()));
                String sectetKey = String.valueOf(sysConfigs.get(SysConfig.QINIU_SECRET_KEY.getKey()));
                String bucket = String.valueOf(sysConfigs.get(SysConfig.QINIU_BUCKET.getKey()));
                String cdn = String.valueOf(sysConfigs.get(SysConfig.QINIU_CDN.getKey()));
                String qiniuStoragePath = String.valueOf(sysConfigs.get(SysConfig.QINIU_STORAGE_PATH.getKey()));
                fileApi= new QiniuApi().init(accessKey,sectetKey,bucket,cdn,qiniuStoragePath);
            break;
            case "ali":
                String aliAccessKey = String.valueOf(sysConfigs.get(SysConfig.ALI_ACCESS_KEY.getKey()));
                String aliSecretKey = String.valueOf(sysConfigs.get(SysConfig.ALI_SECRET_KEY.getKey()));
                String aliBucket = String.valueOf(sysConfigs.get(SysConfig.ALI_BUCKET.getKey()));
                String aliBucketDomain = String.valueOf(sysConfigs.get(SysConfig.ALI_BUCKEt_DOMAIN.getKey()));
                String aliEndpoint = String.valueOf(sysConfigs.get(SysConfig.ALI_ENDPOINT.getKey()));
                String aliStoragePath = String.valueOf(sysConfigs.get(SysConfig.ALI_STORAGE_PATH.getKey()));
                fileApi = new AliApi().init(aliEndpoint,aliAccessKey,aliSecretKey,aliBucketDomain,aliBucket,aliStoragePath);
            break;
            case "youpai":
                String ypBucket = String.valueOf(sysConfigs.get(SysConfig.YP_BUCKET.getKey()));
                String ypBucketDomain = String.valueOf(sysConfigs.get(SysConfig.YP_BUCKET_DOMAIN.getKey()));
                String ypPassword = String.valueOf(sysConfigs.get(SysConfig.YP_PASSWORD.getKey()));
                String ypUserName = String.valueOf(sysConfigs.get(SysConfig.YP_USERNAME.getKey()));
                String ypStoragePath = String.valueOf(sysConfigs.get(SysConfig.YP_STORAGE_PATH.getKey()));
                fileApi = new YouPaiApi().init(ypBucket,ypBucketDomain,ypPassword,ypUserName,ypStoragePath);
            break;
            case "local":

            break;
        }
        if (fileApi==null){
            throw new OssException("系统暂未配置文件服务相关的内容！");
        }
        return fileApi;
    }

    SysFile saveFile(SysFile sysFile,boolean save){
        if (save){
            SysConfigService sysConfigService = SpringContextAware.getBean(SysConfigService.class);
            Map<String ,Object> sysConfigs = sysConfigService.getConfigs();
            String storageType = String.valueOf(sysConfigs.get(SysConfig.STOTAGE_TYPE.getKey()));
            SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
            sysFile.setUserId(sysUser == null ? null : sysUser.getUserId());
            sysFile.setDeptId(sysUser.getDeptId());
            sysFile.setStoreType(storageType);
            SysFileService sysFileService = SpringContextAware.getBean(SysFileService.class);
            sysFileService.insertSysFile(sysFile);
        }
        return sysFile;
    }

    boolean download(FileApi fileApi,String filePath,String storageType){
        SysFileService sysFileService = SpringContextAware.getBean(SysFileService.class);
        SysFile sysFile = sysFileService.getByPathAndStorageType(filePath,storageType);
        String fileKey = filePath;
        CheckIsNull check = new CheckIsNull();
        if (check.objCheckIsNull(sysFile)){
            fileKey = sysFile.getFilePath();
        }
        return fileApi.download(fileKey);
    }
    boolean delete(FileApi fileApi,String filePath,String storageType){
        SysFileService sysFileService = SpringContextAware.getBean(SysFileService.class);
        SysFile sysFile = sysFileService.getByPathAndStorageType(filePath,storageType);
        String fileKey = filePath;
        CheckIsNull check = new CheckIsNull();
        if (check.objCheckIsNull(sysFile)){
            fileKey = sysFile.getFilePath();
        }
        return fileApi.delete(fileKey);
    }

}
